<template>
  <div>
    <!-- 新增排片计划页面 -->
    为
    <b style="color: #409eff">{{ cinemaRoom.cinema_name }}</b>
    的
    <b style="color: #409eff"
      >{{ cinemaRoom.cinema_room_name }} ({{ cinemaRoom.cinema_room_type }})</b
    >
    新增排片计划

    <!-- 分割线 -->
    <el-divider></el-divider>

    <!-- 表单 -->
    <el-form
      style="width: 600px"
      :model="form"
      :rules="rules"
      ref="form"
      label-width="130px"
      class="demo-form"
    >
      <el-form-item label="选择电影" prop="movie_id">
        <el-select
          ref="select"
          filterable
          remote
          reserve-keyword
          :remote-method="remoteMethod"
          :loading="loading"
          style="width: 100%"
          v-model="form.movie_id"
          placeholder="请选择电影"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开播时间" required>
        <el-col :span="11">
          <el-form-item prop="showingon_date">
            <el-date-picker
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
              v-model="form.showingon_date"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col style="text-align: center" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="showingon_time">
            <el-time-picker
              value-format="HH:mm"
              placeholder="选择场次"
              v-model="form.showingon_time"
              style="width: 100%"
              :picker-options="{
                selectableRange: '06:00:00 - 23:59:59',
                format: 'HH:mm',
              }"
            ></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="票价" prop="price">
        <el-input v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="是否立即发布" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()"
          >立即新增排片计划</el-button
        >
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from '@/http'
export default {
  data() {
    return {
      form: {
        cinema_id: '',
        cinema_room_id: this.$route.params.roomId,
        movie_id: '',
        showingon_date: '',
        showingon_time: '',
        status: 1,
        price: '',
      },

      rules: {
        cinema_id: [{ required: true, message: '必选', trigger: 'change' }],
        cinema_room_id: [{ required: true, message: '必填', trigger: 'blur' }],
        movie_id: [{ required: true, message: '必填', trigger: 'blur' }],
        showingon_date: [{ required: true, message: '必填', trigger: 'blur' }],
        showingon_time: [{ required: true, message: '必填', trigger: 'blur' }],
        status: [{ required: true, message: '必填', trigger: 'blur' }],
        price: [{ required: true, message: '必填', trigger: 'blur' }],
      },

      cinemaRoom: {}, // 存储当前选中的放映厅对象
      loading: false, // 表示电影是否正在模糊查询过程中
      options: [], // 存储所有的选项
    }
  },

  methods: {
    /**  */
    remoteMethod(query) {
      this.loading = true
      if (query.trim() != '') {
        // 发送请求，加载相关电影推荐列表
        let params = { name: query, page: 1, pagesize: 10 }
        httpApi.MovieApi.queryAllByNameLike(params).then(res => {
          console.log('模糊查询电影列表', res)
          this.loading = false
          this.options = res.data.data.result
        })
      }
    },

    /** 提交表单 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 验证通过  添加操作
          httpApi.ShowingonPlanApi.add(this.form).then(res => {
            console.log('新增排片计划', res)
            if (res.data.code == 200) {
              // 提示添加成功
              let movieName = this.$refs['select']._data.selectedLabel
              this.$notify({
                title: '成功',
                type: 'success',
                dangerouslyUseHTMLString: true, // 设置支持html标签
                message: `<div>上映电影：《${movieName}》</div>
                          <div>场次时间：
                          ${this.form.showingon_date} - 
                          ${this.form.showingon_time}</div>`,
                duration: 5000,
              })
              // 重置表单
              this.$refs['form'].resetFields()
            }
          })
        }
      })
    },

    /** 加载放映厅的基本信息 */
    loadRoomInfo() {
      let params = { id: this.form.cinema_room_id }
      httpApi.CinemaRoomApi.queryById(params).then(res => {
        console.log('查询放映厅的基本信息', res)
        this.cinemaRoom = res.data.data
        // 为新增排片计划的表单项：cinema_id赋值：
        this.form.cinema_id = this.cinemaRoom.cinema_id
      })
    },
  },

  mounted() {
    // 加载放映厅的基本信息
    this.loadRoomInfo()
  },
}
</script>

<style lang="scss" scoped></style>
